package com.smzd.framework.system.actions;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.framework.annotation.Access;
import com.smzd.framework.core.actions.AbstractMeetingOwnedController;
import com.smzd.framework.core.business.IFGService;
import com.smzd.framework.system.business.GroupService;
import com.smzd.framework.system.business.UserService;
import com.smzd.framework.system.domain.Group;
import com.smzd.framework.system.domain.Meeting;
import com.smzd.framework.system.domain.Role;
import com.smzd.framework.system.domain.User;
import com.smzd.framework.utils.JsonHelper;
import com.smzd.framework.utils.db.ORPage;

@Controller
@RequestMapping("/system/groups")
@Access(Role.TAG_ROLE_GROUPS)
public class GroupController extends AbstractMeetingOwnedController<Group> {
    @Autowired
    private GroupService service;
    @Autowired
    private UserService userService;

    @Override
    protected IFGService<Group> getService() {
        return service;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @Override
    public String toAddPage(Map<String, Object> model, Group elem, BindingResult result) throws Exception {
        return super.toAddPage(model, elem, result);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postAddData(Map<String, Object> model, Group elem, BindingResult result) throws Exception {
        return super.postAddData(model, elem, result);
    }

    @RequestMapping(method = RequestMethod.GET)
    @Override
    public String toListPage(Map<String, Object> model, Group elem, BindingResult result) throws Exception {
        return super.toListPage(model, elem, result);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String getListData(Map<String, Object> model, ORPage<Meeting, Group> page, BindingResult result)
            throws Exception {
        return super.getListData(model, page, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    @Override
    public String toEditPage(Map<String, Object> model, Group elem, BindingResult result) throws Exception {
        elem = service.getItemById(elem.getId());
        return super.toEditPage(model, elem, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postEditData(Map<String, Object> model, Group elem, BindingResult result) throws Exception {
        return super.postEditData(model, elem, result);
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postRemoveData(Map<String, Object> model, Group elem, BindingResult result) throws Exception {
        return super.postRemoveData(model, elem, result);
    }

    // /////////////扩展功能--所属用户组的多对多编辑////////////////////
    /**
     * toUserGroupsPage：导航到目标用户所属用户组的编辑页面。<br>
     * 注意：应当限制只有管理员用户可以进行此项操作！
     * 
     * @param model
     * @param userId
     * @return
     */
    @RequestMapping(value = "/user-{userId}", method = RequestMethod.GET)
    @Access({ Role.TAG_ROLE_USER_MANAGER, Role.TAG_ROLE_USER_OWNER })
    public String toUserGroupsPage(Map<String, Object> model, @PathVariable("userId") int userId) throws Exception {
        User user = userService.getItemById(userId);
        model.put("item", user);
        return getMyView("usergroups.ftl");
    }

    /**
     * getUserGroupsData：获取目标用户的用户组配置列表。<br>
     * 目标用户所属的用户组被排在前面，并且以复选框标志已经属于当前用户组<br>
     * 注意：应当限制只有管理员用户可以进行此项操作！
     * 
     * @param model
     * @param userId
     * @param page
     * @param result
     * @return
     */
    @RequestMapping(value = "/list/user-{userId}", method = RequestMethod.POST)
    @ResponseBody
    @Access({ Role.TAG_ROLE_USER_MANAGER, Role.TAG_ROLE_USER_OWNER })
    public String getUserGroupsData(Map<String, Object> model, @PathVariable("userId") int userId,
            ORPage<Meeting, Group> page, BindingResult result) throws Exception {
        page.setOwner(getOwner());
        page.setExt(String.valueOf(userId));
        return service.getUserGroupListDataJson(page);
    }

    /**
     * saveUserGroups：保存用户所属用户组的改变情况。<br>
     * 只有新改变的部分数据需要提交服务器端进行处理。<br>
     * 注意：应当限制只有管理员用户可以进行此项操作！
     * 
     * @param model
     * @param userId
     * @param groupIds
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit/user-{userId}", method = RequestMethod.POST)
    @ResponseBody
    @Access({ Role.TAG_ROLE_USER_MANAGER, Role.TAG_ROLE_USER_OWNER })
    public String saveUserGroups(Map<String, Object> model, @PathVariable("userId") int userId,
            @RequestParam("groupIds") String groupIds) throws Exception {
        try {
            if (StringUtils.isEmpty(groupIds)) {
                return JsonHelper.getSuccessJsonString("没有任何变动提交！");
            }
            service.saveUserGroups(userId, groupIds);
            return JsonHelper.getSuccessJsonString("保存成功！");
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

}
